﻿using System.Linq;
using System.Data.Linq;

namespace UniMana.Data
{
    public static class LeadershipData
    {
        /// <summary>
        /// GetData
        /// </summary>
        /// <param name="nLeadership"></param>
        /// <returns></returns>

        public static IQueryable GetData(this Table<Leadership> nLeadership)
        {
            var values = from t in nLeadership
                         select t;
            return values;
        }

        /// <summary>
        /// Lấy danh sách khoa quản lý của thành viên
        /// </summary>
        /// <param name="nLeadership"></param>
        /// <param name="m_iLeaderRelationshipKey"></param>
        /// <returns></returns>

        public static IQueryable GetListDepartmentWithLeaderRelationshipKey(this Table<Leadership> nLeadership,
            Table<Catalogue_Department> nCatalogue_Department,
            int m_iLeaderRelationshipKey)
        {
            var values = from t in nLeadership
                         join t1 in nCatalogue_Department on t.Catalogue_DepartmentKey equals t1.Catalogue_DepartmentKey
                         where t.LeadershipRelationshipKey == m_iLeaderRelationshipKey
                         select new
                         {
                             t.LeadershipKey,
                             t.Catalogue_DepartmentKey,
                             t.LeadershipRelationshipKey,
                             t1.Name
                         };
            return values;
        }

        /// <summary>
        /// Kiểm tra khoa cần thêm vào có tồn tại chưa
        /// </summary>
        /// <param name="nLeadership"></param>
        /// <param name="m_iCatalogue_DepartmentKey"></param>
        /// <param name="m_iLeadershipRelationshipKey"></param>
        /// <returns></returns>

        public static bool CheckValueIsExit(this Table<Leadership> nLeadership,
            int m_iCatalogue_DepartmentKey,
            int m_iLeadershipRelationshipKey)
        {
            var values = from t in nLeadership
                         where t.Catalogue_DepartmentKey == m_iCatalogue_DepartmentKey &&
                         t.LeadershipRelationshipKey == m_iLeadershipRelationshipKey
                         select t;
            if (values.Count() > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// Đếm số khoa của người quản lý
        /// </summary>
        /// <param name="nLeadership"></param>
        /// <param name="m_iLeadershipRelationshipKey"></param>
        /// <returns></returns>

        public static int CountDepartmentOfPersonnal(this Table<Leadership> nLeadership,
            int m_iLeadershipRelationshipKey)
        {
            var values = from t in nLeadership
                         where t.LeadershipRelationshipKey == m_iLeadershipRelationshipKey
                         select t;
            if (values.Count() > 0)
                return values.Count();
            else
                return 0;
        }

        /// <summary>
        /// Lấy danh sách mã phân quyền với khoa truyền vào
        /// </summary>
        /// <param name="nLeadership"></param>
        /// <param name="m_iDepartmentKey"></param>
        /// <returns></returns>

        public static IQueryable GetLeadershipRelationshipKeyWithDepartmentKey(this Table<Leadership> nLeadership,
            int m_iDepartmentKey)
        {
            var values = from t in nLeadership
                         where t.Catalogue_DepartmentKey == m_iDepartmentKey
                         select new
                         {
                             t.LeadershipRelationshipKey
                         };
            return values.Distinct();
        }
    }
}
